package com.roam.mapper;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.*;
import com.mongodb.client.model.Sorts;
import com.roam.VO.DisplaySceneVO;
import com.roam.VO.PopularSceneVO;
import com.roam.mgdb.MongoUtils;
import jakarta.annotation.Resource;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

@Component
public class HomePageMapper {


    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    MongoUtils mongoUtils = new MongoUtils();

    public ArrayList<DisplaySceneVO> getDisplayScenes(int n) {
        ArrayList<DisplaySceneVO> displaySceneVOS = new ArrayList<>();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MMMM/d", Locale.ENGLISH);
        // 格式化当前时间
        String time = now.format(formatter);

        String key = "getDisplayScenes4" + "_" + time;
        if (redisTemplate.hasKey(key)) {
            displaySceneVOS = (ArrayList<DisplaySceneVO>) valueOperations.get(key);
            if (!displaySceneVOS.isEmpty()) {
                return displaySceneVOS;
            }
        }
        List<String> cities = new ArrayList<>() {{
            add("深圳");
            add("北京");
            add("上海");
            add("丽江");
        }};
        List<Document> result = getResult(4, cities);

        for (Document document : result) {
            String name = document.getString("basicInfo:name");
            String city = document.getString("basicInfo:city");
            int id = document.getInteger("basicInfo:id");

            List<String> urlList = (List<String>) document.get("imgs:imgs");
            String url = "";
            if (urlList != null && !urlList.isEmpty()) {
                url = urlList.get(0);
            }
            //取出来的url格式为“(https://dimg04.c-ctrip.com/images/10090n000000e2tjiAAF1_D_521_391.jpg)”
            //再截取()中间的字符串
            if (url.length() > 1) {
                url = url.substring(1, url.length() - 1);
            } else {
                // 字符串为空或只有一个字符，无需操作
                url = "";
            }

            String time1=time+" "+name;

            DisplaySceneVO displaySceneVO = DisplaySceneVO.builder()
                    .scene(name)
                    .url(url)
                    .time(time1)
                    .city(city)
                    .sceneID(id)
                    .build();

            displaySceneVOS.add(displaySceneVO);
        }
        valueOperations.set(key, displaySceneVOS, 1, TimeUnit.DAYS);

        return displaySceneVOS;

    }

    public ArrayList<PopularSceneVO> getPopularScenes(int n) {
        ArrayList<PopularSceneVO> popularSceneVOS = new ArrayList<>();
        String key = " getPopularScenes";
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        if (redisTemplate.hasKey(key)) {
            popularSceneVOS = (ArrayList<PopularSceneVO>) valueOperations.get(key);
            if (!popularSceneVOS.isEmpty()) {
                return popularSceneVOS;
            }
        }


        List<String> cities = new ArrayList<>() {{
            add("杭州");
            add("台北");
            add("重庆");
            add("厦门");
            add("哈尔滨");
        }};
        List<Document> result = getResult(20, cities);

        for (Document document : result) {
            String name = document.getString("basicInfo:name");
            String position = document.getString("basicInfo:position");
            String city = document.getString("basicInfo:city");
            int id = document.getInteger("basicInfo:id");

            List<String> introduceList = (List<String>) document.get("detailInfo:introduce");
            String introduction = "";
            if (introduceList != null && !introduceList.isEmpty()) {
                introduction = introduceList.get(0);
            }
            // 取前十个字符
            String firstTenChars = introduction.substring(0, Math.min(introduction.length(), 50)) + "...";


            List<String> urlList = (List<String>) document.get("imgs:imgs");
            String url = "";
            if (urlList != null && !urlList.isEmpty()) {
                url = urlList.get(0);
            }
            //取出来的url格式为“(https://dimg04.c-ctrip.com/images/10090n000000e2tjiAAF1_D_521_391.jpg)”
            //再截取()中间的字符串
            if (url.length() > 1) {
                url = url.substring(1, url.length() - 1);
            } else {
                // 字符串为空或只有一个字符，无需操作
                url = "";
            }

            PopularSceneVO scene = PopularSceneVO.builder()
                    .scene(name)
                    .location(position)
                    .introduction(firstTenChars)
                    .url(url)
                    .city(city)
                    .sceneID(id)
                    .build();

            popularSceneVOS.add(scene);
        }
        valueOperations.set(key, popularSceneVOS, 1, TimeUnit.DAYS);
        return popularSceneVOS;
    }

    public List<Document> getResult(int n, List<String> cities) {

        List<Document> result = new ArrayList<>();
        for (String city : cities) {
            for (Document document : mongoUtils.getHeatDescendingData(n / cities.size(), city))
                result.add(document);
        }
        return result;

    }
}


